home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1988 / 04 / dc.c < prev    next >
Text File  |  1988-04-28  |  4KB  |  160 lines

  1. /* 
  2.    Resident segment for the Windows digital clock
  3. */
  4.  
  5. #include <windows.h>
  6. #include <dos.h>
  7. #include <string.h>
  8.  
  9. #define EXTERN        /* all global variables declared in this module */
  10. #include "dc.h"
  11.  
  12. static struct dostime_t time;    /* holds the time parameters */
  13. static RECT clrect;        /* formatting rectangle for time display */
  14.  
  15. /* local function declarations */
  16. static void NEAR MainWndPaint(HWND, LPPAINTSTRUCT);
  17.  
  18. /* Entry point for program */
  19. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  20. HANDLE hInstance, hPrevInstance;
  21. LPSTR lpszCmdLine;
  22. int cmdShow;
  23. {
  24.  
  25.     MSG msg;
  26.  
  27.   /* If initialization is not successful then exit */
  28.     if (!InitProgram(hInstance,hPrevInstance, lpszCmdLine, cmdShow))
  29.     return FALSE;
  30.  
  31.   /* Retrieve messages from Windows */
  32.     while (GetMessage((LPMSG)&msg,NULL,0,0)) {
  33.     TranslateMessage((LPMSG)&msg);
  34.     DispatchMessage((LPMSG)&msg);
  35.     }
  36.     return msg.wParam;        /* exit program */
  37. }
  38.  
  39. /* All messages are processed here */
  40. long FAR PASCAL MainWndProc(hWnd,message,wParam,lParam)
  41. HWND hWnd;
  42. unsigned message;
  43. WORD wParam;
  44. LONG lParam;
  45. {
  46.  
  47.     PAINTSTRUCT ps;
  48.  
  49.     static BYTE prevminute = 0;        /* these values control updating */
  50.     static BYTE prevhour = 0;
  51.  
  52.     switch(message) {
  53.       /* initialize the time structure when window is created */
  54.     case WM_CREATE:
  55.         _dos_gettime(&time);
  56.         break;
  57.  
  58.       /* we are exiting, so kill the timer and post a quit message */
  59.     case WM_DESTROY:
  60.         KillTimer(hWnd, ClockTimerEventID);
  61.         PostQuitMessage(0);
  62.         break;
  63.  
  64.       /* keep a record of the window size for use by the paint function */
  65.     case WM_SIZE:
  66.         clrect.right = LOWORD(lParam);
  67.         clrect.bottom = HIWORD(lParam);
  68.         break;
  69.  
  70.       /* Each timer event comes here for processing */
  71.     case WM_TIMER:
  72.         _dos_gettime(&time);
  73.       /* if new hour, erase window and redisplay the time */
  74.         if (prevhour != time.hour)
  75.         InvalidateRect(hWnd, (LPRECT)NULL, TRUE);
  76.       /* otherwise, just redisplay the time without erasing background */
  77.         else {
  78.         if (IsIconic(hWnd)) {
  79.           /* but, when iconic, do this only on a minute change */
  80.             if (prevminute != time.minute)
  81.                 InvalidateRect(hWnd, (LPRECT)NULL, FALSE); 
  82.         }
  83.           /* and if not iconic, display every second */
  84.         else
  85.             InvalidateRect(hWnd, (LPRECT)NULL, FALSE); 
  86.         }
  87.         prevminute = time.minute;    /* save the new values */
  88.         prevhour = time.hour;
  89.         break;
  90.         
  91.       /* we have to update the window */
  92.     case WM_PAINT:
  93.         BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  94.         MainWndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  95.         EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  96.         break;
  97.  
  98.       /* all other messages come here */
  99.     default:
  100.         return ((long)DefWindowProc(hWnd,message,wParam,lParam));
  101.         break;
  102.     }
  103.     return(0L);
  104. }
  105.  
  106. /* Create a text string with the time and display it */
  107. static void NEAR MainWndPaint(hWnd, lpps)
  108. HWND hWnd;
  109. LPPAINTSTRUCT lpps;
  110. {
  111.  
  112.     char szTimeText[40];
  113.     int i;
  114.     BOOL iconic = IsIconic(hWnd);
  115.     BYTE hour = time.hour;
  116.     BYTE minute = time.minute;
  117.     BYTE second = time.second;
  118.  
  119.     if (hour > 12)             /* 12-hour display, i. e. 12:30 not 00:30 */
  120.     hour -= 12;
  121.     if (hour < 1)
  122.     hour = 12;
  123.  
  124.     if (iconic) {            
  125.     strcpy(szTimeText, szTimestr);        /* Give icon display a title */
  126.      i = strlen(szTimeText);
  127.     szTimeText[i++] = '\n';            /* Force a new line */
  128.         SelectObject(lpps->hdc, hFont);     /* Select in the small font */
  129.     }
  130.     else
  131.     i = 0;
  132.  
  133.     if (hour > 9)                /* ...hour...*/
  134.     szTimeText[i++] = '1';
  135.     szTimeText[i++] = hour % 10 + '0';
  136.     szTimeText[i++] = ':';            /* ...minute...    */
  137.     szTimeText[i++] = minute > 9 ? minute / 10 + '0' : '0';
  138.     szTimeText[i++] = minute % 10 + '0';
  139.     if (!iconic) {
  140.     szTimeText[i++] = ':';            /* ...second...*/
  141.     szTimeText[i++] = second > 9 ? second / 10 + '0': '0';
  142.     szTimeText[i++] = second % 10 + '0';
  143.     }
  144.     if (iconic)
  145.         szTimeText[i++] = '\n';        /* force another line if iconic */
  146.     else
  147.         szTimeText[i++] = ' ';        /* otherwise make a space */
  148.     szTimeText[i] = '\0';
  149.  
  150.     if (time.hour > 11)            /* morning or afternoon */
  151.     strcat(szTimeText, szPM);
  152.     else
  153.     strcat(szTimeText, szAM);
  154.  
  155.   /* display the text */
  156.     DrawText(lpps->hdc, (LPSTR)szTimeText, -1, (LPRECT)&clrect,
  157.                  DT_NOCLIP | DT_EXTERNALLEADING | DT_CENTER);
  158.  
  159. }
  160.